package com.sh.data.engine.repository.dao.integration.online;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sh.data.engine.common.enumDefinition.OfflineSyncTaskStatus;
import com.sh.data.engine.repository.dao.integration.online.entity.FlinkDataSyncTaskEntity;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Component
public interface FlinkDataSyncTaskMapper extends BaseMapper<FlinkDataSyncTaskEntity> {


    default List<FlinkDataSyncTaskEntity> getSummaryList(Long projectId) {

        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query.eq(FlinkDataSyncTaskEntity::getProjectId, projectId);

        return this.selectList(query);
    }

    default List<FlinkDataSyncTaskEntity> checkTaskName(String taskName, Long projectId) {

        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query
            .eq(FlinkDataSyncTaskEntity::getProjectId, projectId)
            .eq(FlinkDataSyncTaskEntity::getTaskName, taskName);
        return this.selectList(query);
    }

    default List<FlinkDataSyncTaskEntity> getAllPublishTask() {
        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query.eq(FlinkDataSyncTaskEntity::getTaskStatus, 2);
        return this.selectList(query);
    }

    default List<FlinkDataSyncTaskEntity> getAllPublishTask(Long projectId) {

        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query
            .eq(FlinkDataSyncTaskEntity::getTaskStatus, 2)
            .eq(FlinkDataSyncTaskEntity::getProjectId, projectId);

        return this.selectList(query);
    }

    default Boolean checkDirIsEmpty(Collection<Long> ids) {
        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query.in(FlinkDataSyncTaskEntity::getDirId, ids);
        List<FlinkDataSyncTaskEntity> multi = selectList(query);
        return multi.size() == 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    default FlinkDataSyncTaskEntity getByRecordId(Long recordId) {
        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query.eq(FlinkDataSyncTaskEntity::getRecordId, recordId);
        FlinkDataSyncTaskEntity flinkDataSyncTaskEntity = this.selectOne(query);
        if (Objects.isNull(flinkDataSyncTaskEntity)) {
            flinkDataSyncTaskEntity = this.selectById(recordId);
        }
        return flinkDataSyncTaskEntity;
    }

    default List<FlinkDataSyncTaskEntity> getMqTaskRunningList() {

        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query
            .eq(FlinkDataSyncTaskEntity::getTaskStatus, OfflineSyncTaskStatus.running.getTaskStatus())
            .and(
                i ->
                    i.eq(FlinkDataSyncTaskEntity::getSourceDsType, "Kafka")
                        .or()
                        .eq(FlinkDataSyncTaskEntity::getSourceDsType, "Mqtt"));
        return this.selectList(query);
    }

    default List<FlinkDataSyncTaskEntity> getFlinkTaskRunList() {
        LambdaQueryWrapper<FlinkDataSyncTaskEntity> query =
            new QueryWrapper<FlinkDataSyncTaskEntity>().lambda();
        query
            .eq(FlinkDataSyncTaskEntity::getTaskStatus, 2)
            .isNotNull(FlinkDataSyncTaskEntity::getRecordId);
        return this.selectList(query);
    }
}